﻿using Butcher.winform.EnterpriseForms.Eeductiones;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class LiveAnimalweighings : Form
    {
        public LiveAnimalweighings()
        {
            InitializeComponent();
            tb_GrossWeight.LostFocus += new EventHandler(tb_GrossWeight_GotFocus);

            GetSlaughtercCircle();
            getList();
        }

        Approach babab = new Approach();
        Scale ScaleInfo = new Scale();
        /// <summary>
        /// 绑定待宰圈下拉框
        /// </summary>
        public void GetSlaughtercCircle()
        {
            string url = @"http://localhost:5229/api/Section/WaitingCircleHtml";
            HttpClient client = new HttpClient();
            WaitingCirclePagDTO res = new WaitingCirclePagDTO();
            res.pagesize = 50;
            res.pageindex = 1;
            res.InChargeofName = null;
            res.Slaugthertype = null;
            var content = new StringContent(JsonSerializer.Serialize<WaitingCirclePagDTO>(res));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["lists"].Value<JArray>();
            List<SlaughterCircleSelectDTO> enterprises = aaa.ToObject<List<SlaughterCircleSelectDTO>>();
            cb_SlaughterCircleId.DataSource = enterprises;
            cb_SlaughterCircleId.DisplayMember = "slaughterCircleName";
            cb_SlaughterCircleId.ValueMember = "slaughterCircledId";
        }

        public void getList()
        {
            var ApproachValue = ApproachBackfill.value;
            if (!string.IsNullOrEmpty(ApproachValue))
            {
                babab = Json.Deserialize<Approach>(ApproachValue);
                tb_ApproachNo.Text = babab.ApproachNo;
                tb_licensePlate.Text = babab.licensePlate;
                Suppliers.Text = babab.Suppliers;
                SettlementType.Text = babab.SettlementType;
                Address.Text = babab.Address;
                tb_Approachnum.Text = Convert.ToString(babab.Approachnum);
                ApproachBatch.Text = babab.ApproachBatch;
            }

            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                ui_tare.Text = ScaleInfo.TareWeightDeduction.ToString();
            }
            string url = @"http://localhost:5229/api/Enterprise/LiveAddList";
            HttpClient client = new HttpClient();
            string result = client.GetStringAsync(url).Result;
            JObject info = JObject.Parse(result);
            var aaa = info["date"].Value<JArray>();
            List<LiveAnimalWeighingListDTO> enterprises = aaa.ToObject<List<LiveAnimalWeighingListDTO>>();
            dataGridView1.DataSource = enterprises;
        }

        private void uiSymbolButton1_Click_1(object sender, EventArgs e)
        {
            ScaleBackfill scalear = new ScaleBackfill();
            scalear.Owner = this;
            this.Hide();
            scalear.ShowDialog();
            Application.ExitThread();
        }

        private void uiSymbolButton1_Click_2(object sender, EventArgs e)
        {
            ScaleBackfill.pid = 1;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Owner = this;
            scale.Hide();
            scale.ShowDialog();
            Application.ExitThread();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            ApproachBackfill appro = new ApproachBackfill();
            appro.Owner = this;
            appro.Hide();
            appro.ShowDialog();
            Application.ExitThread();
        }

        public void tb_GrossWeight_GotFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ui_tare.Text))
            {
                ui_NetWeight.Text = (Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text)).ToString();
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            LiveAnimalweighing live = new LiveAnimalweighing();
            live.ApproachId = babab.ApproachId;
            live.LiveAnimalweighingNo = "";
            live.ApproachNo = tb_ApproachNo.Text;
            live.GrossWeight = Convert.ToDecimal(tb_GrossWeight.Text);
            live.Tare = Convert.ToDecimal(ui_tare.Text);
            live.NetWeight = Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text);
            live.AverageWeight = Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text);
            live.WeightNum = Convert.ToInt32(tb_SlaughterCircleNum.Text);
            live.SlaughterCircleId = Convert.ToInt32(cb_SlaughterCircleId.SelectedValue);
            live.SlaughterCircleNum = Convert.ToInt32(tb_SlaughterCircleNum.Text);
            live.Remark = rb_Remark.Text;
            live.WeighTime = DateTime.Now;
            live.Deductions = DeductionsBackfill.Deuvalue;
            live.Scaleid = ScaleInfo.Scaleid;
            string url = @"http://localhost:5229/api/Enterprise/AddLiveAnimaWeighing";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<LiveAnimalweighing>(live));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            if (info != null)
            {
                MessageBox.Show("新增成功");
                LiveAnimalweighingList str = new LiveAnimalweighingList();
                str.Owner = this;
                this.Hide();
                str.ShowDialog();
                Application.ExitThread();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DeductionsBackfill dedu = new DeductionsBackfill();
            this.Hide();
            dedu.ShowDialog();
        }
    }
}
